package com.kt.mes.job;

import com.kt.mes.opc.OpcInteface;
import com.kt.mes.util.OpcUtil;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.util.ScheduleUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("findOpcServerJob")
public class FindOpcServerJob{

    @Autowired
    private OpcInteface opcInteface;

    @Autowired
    private Scheduler scheduler;

    public void findServer(String gw){

        switch (gw){
            case "gw01":
                System.out.println("重新连接 工位1");
                opcInteface.getCon("gw01", OpcUtil.opcUrlGw01);

                if(OpcUtil.mapOpcClient.get("gw01")!=null){
                    opcInteface.subscriptionOfGw("gw01");
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw01");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位1  失败");
                }

                break;
            case "gw02":
                System.out.println("重新连接 工位2");

                opcInteface.getCon("gw02", OpcUtil.opcUrlGw02);

                if(OpcUtil.mapOpcClient.get("gw02")!=null){
                    opcInteface.subscriptionOfGw("gw02");;
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw02");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位2  失败");
                }
                break;
            case "gw03":
                System.out.println("重新连接 工位3");

                opcInteface.getCon("gw03", OpcUtil.opcUrlGw03);

                if(OpcUtil.mapOpcClient.get("gw03")!=null){
                    opcInteface.subscriptionOfGw("gw03");
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw03");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位3  失败");
                }
                break;
            case "gw04":
                System.out.println("重新连接 工位4");

                opcInteface.getCon("gw04", OpcUtil.opcUrlGw04);

                if(OpcUtil.mapOpcClient.get("gw04")!=null){
                    opcInteface.subscriptionOfGw("gw04");
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw04");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位4  失败");
                }
                break;
            case "gw05":
                System.out.println("重新连接 工位5");

                opcInteface.getCon("gw05", OpcUtil.opcUrlGw05);

                if(OpcUtil.mapOpcClient.get("gw05")!=null){
                    opcInteface.subscriptionOfGw("gw05");
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw05");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位5  失败");
                }
                break;
            case "gw06":
                System.out.println("重新连接 工位6");

                opcInteface.getCon("gw06", OpcUtil.opcUrlGw06);

                if(OpcUtil.mapOpcClient.get("gw06")!=null){
                    opcInteface.subscriptionOfGw("gw06");
                    //暂停任务
                    SysJob job= (SysJob)OpcUtil.mapOpcJob.get("gw06");
                    job.setStatus("1");
                    try {
                        ScheduleUtils.createScheduleJob(scheduler, job);
                    } catch (SchedulerException schedulerException) {
                        schedulerException.printStackTrace();
                    } catch (TaskException taskException) {
                        taskException.printStackTrace();
                    }

                }else {
                    System.out.println("重新连接 工位6  失败");
                }
                break;
        }


    }

}
